package com.sinoglobal.dumping.activity;

import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewStub;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.adapter.Dumpling_CashDumplingDetailAdapter;
import com.sinoglobal.dumping.api.Dumpling_RemoteImpl;
import com.sinoglobal.dumping.base.Dumpling_ShareAbstractActivity;
import com.sinoglobal.dumping.base.Dumpling_SinoAsyncTask;
import com.sinoglobal.dumping.base.Dumpling_SinoConfig;
import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.bean.Dumpling_BaseVo;
import com.sinoglobal.dumping.bean.Dumpling_ShareInfoResultList;
import com.sinoglobal.dumping.bean.myDumpling.Dumpling_CashDumplingDetailBean;
import com.sinoglobal.dumping.util.Dumpling_HttpMethodSet;
import com.sinoglobal.dumping.view.Dumpling_LimitHeightListView;
import com.sinoglobal.wallet.activity.W_HomeActivity;
import com.umeng.socialize.bean.SHARE_MEDIA;

public class Dumpling_MyCashDumplingDetailActivity extends Dumpling_ShareAbstractActivity {

	
	private Dumpling_LimitHeightListView lv_cash_dumpling_detail;
	private TextView userPhone;
	private TextView stateDescription;
	private Dumpling_CashDumplingDetailAdapter dumpling_CashDumplingDetailAdapter;
	private String dumplingUserPutmoneytid;
	private int status ;
	private int statusHeight;
	private int textviewHeight;
	private int mDrawHeight;
	private ImageView iv_left;
	private ImageView iv_right;
	private TextView tv_des;
	private RelativeLayout rl_listview_rootview;
	private RelativeLayout rl_status_rootview;
	private int mStatusBarHeight;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		  View view = getView(R.id.title_right_btn_share);
	      hideView(mTemplateRightText, mTemplateRightImg,view);
	      mTemplateTitleText.setText("捞一捞");
	      setContentView(R.layout.dumpling_activity_cash_dumpling_detail);
	      
	      dumplingUserPutmoneytid = getIntent().getExtras().getString("dumplingUserPutmoneytid", "");
	      status = getIntent().getExtras().getInt("getStatus");
	      
	      init();
	      
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (hasFocus) {
			// 获取绘制区域高度    
			Rect outRect = new Rect();  
			getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);  
			mDrawHeight = outRect.height();  
			 
			getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);  
			mStatusBarHeight = outRect.height();
			
			// 设置 左右红条的高度
			RelativeLayout.LayoutParams layoutParams_left = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
			layoutParams_left.height = mDrawHeight - mTemplateHeight -  statusHeight - textviewHeight - 24;
			layoutParams_left.topMargin = textviewHeight ;
			layoutParams_left.addRule(RelativeLayout.ALIGN_PARENT_LEFT, -1);
			RelativeLayout.LayoutParams layoutParams_right = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
			layoutParams_right.height = mDrawHeight- mTemplateHeight - statusHeight - textviewHeight - 24;
			layoutParams_right.topMargin = textviewHeight ;
			layoutParams_right.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, -1);
			iv_right.setLayoutParams(layoutParams_right);
			iv_left.setLayoutParams(layoutParams_left);
		}
	}
	
	private void init() {
		initView();
	
		initAdapter();
	
		if (status != 1) {
			initData();
		}
	}

	private void initView() {
		
		  userPhone = (TextView) findViewById(R.id.cash_user_phone);
		  userPhone.setText(Dumpling_SinoConstans.USER_NAME);
	      stateDescription = (TextView) findViewById(R.id.cash_state_description);
	      rl_listview_rootview = (RelativeLayout) findViewById(R.id.rl_listview_rootview);
	      rl_status_rootview = (RelativeLayout) findViewById(R.id.rl_status_rootview);
	      rl_listview_rootview.setVisibility(status == 1 ? View.GONE : View.VISIBLE);
	      
	      iv_left = (ImageView) findViewById(R.id.iv_left);
	      iv_right = (ImageView) findViewById(R.id.iv_right);
	      tv_des = (TextView) findViewById(R.id.tv_des);
	      lv_cash_dumpling_detail = (Dumpling_LimitHeightListView) findViewById(R.id.lv_cash_dumpling_detail);
	      
	      showWhichViewStub();
	      getMiddleTextViewHeight();
	      getStatusViewHeight();
		
	}

	/**
	 * 测量 状态视图的高度
	 */
	private void getStatusViewHeight() {
		   rl_status_rootview.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
				
				@SuppressLint("NewApi")
				@Override
				public void onGlobalLayout() {
					rl_status_rootview.getViewTreeObserver().removeGlobalOnLayoutListener(this);    
					statusHeight = rl_status_rootview.getHeight();
				}
			});
	}

	/**
	 * 获取 中间textview 的高度
	 */
	private void getMiddleTextViewHeight() {
	    int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 
	    int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 
	    tv_des.measure(w, h); 
	    textviewHeight = tv_des.getMeasuredHeight(); 
	}

	/**
	 * 显示 状态视图
	 */
	private void showWhichViewStub() {
		if (status == 3) {//未领完
	    	  	stateDescription.setText(getResources().getText(R.string.dumpling_cash_state_description_go_on));
	             ViewStub shareInflate= (ViewStub) findViewById(R.id.cash_status_share_inflate);
	             View shareAfterInflate=shareInflate.inflate();
	             shareAfterInflate.setVisibility(View.VISIBLE);
	             Button shareWX= (Button) shareAfterInflate.findViewById(R.id.cash_share_wx);
	             Button shareQQ= (Button) shareAfterInflate.findViewById(R.id.cash_share_qq);
	             shareWX.setOnClickListener(this);
	             shareQQ.setOnClickListener(this);
		} else if (status == 4) { //吃光了
			  	stateDescription.setText(getResources().getText(R.string.dumpling_cash_state_description_none));
	             ViewStub starInflate= (ViewStub) findViewById(R.id.cash_status_star_inflate);
	             View starAfterInflate=starInflate.inflate();
	             starAfterInflate.setVisibility(View.VISIBLE);
	             Button star= (Button) starAfterInflate.findViewById(R.id.cash_star);
	             star.setOnClickListener(this);
		} else if (status == 5) {//已过期
			   stateDescription.setText(getResources().getText(R.string.dumpling_cash_state_description_back));
               ViewStub expiredInflate = (ViewStub) findViewById(R.id.cash_status_expired_inflate);
               View expiredAfterInflate = expiredInflate.inflate();
               expiredAfterInflate.setVisibility(View.VISIBLE);
               Button onceMore = (Button) expiredAfterInflate.findViewById(R.id.cash_share_once_more);
               Button myWallet = (Button) expiredAfterInflate.findViewById(R.id.cash_share_my_wallet);
               onceMore.setOnClickListener(this);
               myWallet.setOnClickListener(this);
		} else if (status == 0) {//领取成功
//			  	stateDescription.setText(getResources().getText(R.string.cash_state_description_success));
//	             ViewStub successInflate= (ViewStub) findViewById(R.id.cash_status_success_inflate);
//	             View successAfterInflate=successInflate.inflate();
//	             successAfterInflate.setVisibility(View.VISIBLE);
//	             TextView stateNumber= (TextView) successAfterInflate.findViewById(R.id.cash_state_number);
//	             String cash="0.88元";
//	             SpannableString styledText = new SpannableString(cash);
//	             int length=cash.length();
//	             styledText.setSpan(new TextAppearanceSpan(this, R.style.dumpling_cash_num), 0, length-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//	             styledText.setSpan(new TextAppearanceSpan(this, R.style.dumpling_cash_unit), length-1, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//	             stateNumber.setText(styledText, TextView.BufferType.SPANNABLE);
//	             Button goOn= (Button) successAfterInflate.findViewById(R.id.cash_go_on);
//	             Button check= (Button) successAfterInflate.findViewById(R.id.cash_check);
//	             goOn.setOnClickListener(this);
//	             check.setOnClickListener(this);
		} else if (status == 1) { // 未发送
				stateDescription.setText(getResources().getText(R.string.dumpling_cash_state_description_not_send));
	            ViewStub notSendInflate= (ViewStub) findViewById(R.id.cash_status_not_send_inflate);
	            View notSendAfterInflate= notSendInflate.inflate();
	            notSendAfterInflate.setVisibility(View.VISIBLE);
	            Button shareWX= (Button) notSendAfterInflate.findViewById(R.id.cash_share_wx_one);
	            Button shareQQ= (Button) notSendAfterInflate.findViewById(R.id.cash_share_qq_one);
	            shareWX.setOnClickListener(this);
	            shareQQ.setOnClickListener(this);
		}
	}

	private void initAdapter() {
		if (dumpling_CashDumplingDetailAdapter == null ) {
			dumpling_CashDumplingDetailAdapter = new Dumpling_CashDumplingDetailAdapter(this);
		}
		lv_cash_dumpling_detail.setAdapter(dumpling_CashDumplingDetailAdapter);
	}

	private void initData() {
		  new Dumpling_SinoAsyncTask<Object, Void, Dumpling_CashDumplingDetailBean>(this, false,true) {

	            @Override
	            public Dumpling_CashDumplingDetailBean before(Object... params) throws Exception {
	            	Dumpling_BaseVo.initPageData();
	                return Dumpling_RemoteImpl.getInstance().queryCashdumplingDetail(Dumpling_SinoConstans.USER_ID, dumplingUserPutmoneytid, 1, 10);
	            }

	            @Override
	            public void after(Dumpling_CashDumplingDetailBean data) {
//	            	List<Dumpling_CashDumplingDetail> lists = new ArrayList<Dumpling_CashDumplingDetail>();
//	            	for (int i = 0; i < 3; i++) {
//						lists.add(new Dumpling_CashDumplingDetailBean().new Dumpling_CashDumplingDetail());
//					}
//	            	
//	            	dumpling_CashDumplingDetailAdapter.setData(lists);
	            	if (data != null && data.resultList != null &&  data.resultList.size() > 0 ) {
	            		dumpling_CashDumplingDetailAdapter.setData(data.resultList);
					}
	            }

	            @Override
	            public void exception() {

	            }
	        }.execute();
	}
	
	/**
	 * 防暴力点击事件
	 */
	@Override
	public void onAntiViolence(View v) {
		
		 if (v.getId()==R.id.cash_share_wx){ //微信分享  第二次分享
			 sharePlatform(SHARE_MEDIA.WEIXIN );
	      }else if(v.getId()==R.id.cash_share_qq){//QQ分享 第二次分享
	    	  sharePlatform(SHARE_MEDIA.QQ);
	      }else if(v.getId()==R.id.cash_star){ //捞饺子  再去包一个
	    	  goIntent(Dumpling_MakeDumplingActivity.class);
	      }else if(v.getId()==R.id.cash_go_on){ //继续捞
	          goIntent(Dumpling_MainActivity.class);
	      }else if(v.getId()==R.id.cash_check){//查看我的饺子
	          goIntent(Dumpling_MyDumplingActivity.class);
	      }else if (v.getId() == R.id.cash_share_once_more) {//再去包一个
	          goIntent(Dumpling_MakeDumplingActivity.class);
	      }else if (v.getId() == R.id.cash_share_my_wallet) {//我的钱包
	    	  try {
	    	      goIntent(W_HomeActivity.class);
	    	  	} catch (Exception e) {
				 e.printStackTrace();
	    	  	}
	      } else if (v.getId() == R.id.cash_share_wx_one) {// 微信分享  第一次分享
	    	  sharePlatform(SHARE_MEDIA.WEIXIN );
		  } else if (v.getId() == R.id.cash_share_qq_one) {//QQ分享 第一次分享
			  sharePlatform(SHARE_MEDIA.QQ);
		}
	}

	/**
	 *  分享平台
	 * @param platform
	 */
	private void sharePlatform(SHARE_MEDIA platform) {
		if (Dumpling_SinoConfig.sMakeShare != null) {
			Dumpling_ShareInfoResultList result = Dumpling_SinoConfig.sMakeShare.getResultList();
			setShare(result.getContent(), result.getTitle(), platform, result.getUrl() + "/?name=" + Dumpling_SinoConstans.USER_NAME + "&productCode=" + Dumpling_SinoConstans.PRODUCT_CODE + "&pannikin_id=" + dumplingUserPutmoneytid, 0, result.getPicurl());
		}else{
			showToast("网络不好，请稍后再试");
			Dumpling_HttpMethodSet.getShareContent(Dumpling_MyCashDumplingDetailActivity.this, Dumpling_MainActivity.TEMP_ID_MAKE, "", "", "", "");
		}
	}

	/**
	 *  分享成功  返回到 我的饺子页面
	 */
	@Override
	protected void shareSuccess() {
		Dumpling_HttpMethodSet.changeStatus(Dumpling_MyCashDumplingDetailActivity.this, dumplingUserPutmoneytid);
		showShortToast(getString(R.string.share_ok));
		goIntent(Dumpling_MyDumplingActivity.class);
	}
}
